package com.kuang.springcloud.controller;

import com.kuang.springcloud.pojo.Dept;
import com.kuang.springcloud.service.DeptService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.apache.logging.log4j.LoggingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

//提供Restful服务
@RestController
//@RequestMapping("/DeptController")
public class DeptController {

    @Autowired
    private DeptService deptService;


    //根据id查询部门

    @RequestMapping(value = "/dept/get/{id}", method = RequestMethod.GET)
    //只要失败了就去调用(它)方法
    @HystrixCommand(fallbackMethod = "hystrixGet")
    public Dept queryById(@PathVariable("id") Integer id) {
        System.out.println("8011的Hystrix端口查询id");
        Dept dept = deptService.queryById(id);
        if (dept == null) {
//            throw new RuntimeException("ID为==>" + id + "不存在该用户,或者信息无法找到");
            throw new LoggingException("ID为==>" + id + "不存在该用户,或者信息无法找到");
        }
        return dept;
    }

    //备选方案
    public Dept hystrixGet(@PathVariable("id") Integer id,Throwable exception) {
        return new Dept().setDeptno(id)
                .setDname("ID为==>" + id + "没有对应的信息,null--@Hystrix:::捕获的异常为::"+exception)
                .setDb_source("no this database in MySQL");
    }

}
